<template>
	<view class="normal-login-container">
		<view class="login-form-content">
			<view class="input-item flex align-center">
				<view class="iconfont icon-user icon"></view>
				<input v-model="loginForm.username" class="input" type="text" placeholder="请输入账号" maxlength="30" />
			</view>
			<view class="input-item flex align-center">
				<view class="iconfont icon-password icon"></view>
				<input v-model="loginForm.password" type="password" class="input" placeholder="请输入密码" maxlength="20" />
			</view>


			<view class="action-btn">
				<button @click="handleLogin" class="login-btn cu-btn block bg-blue lg round">登录</button>
			</view>
			<view class="reg text-center">
				<text class="text-grey1">没有账号？</text>
				<text @click="gotoRegister" class="text-blue">立即注册</text>
			</view>
			<view class="xieyi text-center">
				<text class="text-grey1">登录即代表同意</text>
				<text class="text-blue">《用户协议》</text>
				<text class="text-blue">《隐私协议》</text>
			</view>
		</view>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				userlist: [],
				loginForm: {
					username: "admin",
					password: "123456",
				}
			}
		},
		created() {
			this.userlist = uni.getStorageSync('userlist')
			console.log(this.userlist);
		},
		methods: {
			// 登录方法验证，账号密码输入验证
			handleLogin() {
				if (this.loginForm.username === "") {
					uni.showToast({
						title: "请输入您的账号",
						icon: 'none'
					})
				} else if (this.loginForm.password === "") {
					uni.showToast({
						title: "请输入您的密码",
						icon: 'none'
					})
				} else {
					this.pwdLogin()
				}
			},
			// 密码登录判断用户账号密码是否正常
			pwdLogin() {
				// 验证密码
				// 判断用户列表是否包含该用户对象
				this.userlist.map(item => {
					// 如果账号密码都正确，登录成功，跳转主页
					if (item.username === this.loginForm.username && item.password === this.loginForm.password) {
						// 保存当前登录对象信息，同时进行数据持久化保存
						getApp().globalData.loginUser = item
						uni.setStorageSync('loginUser', item);

						//跳转到 tabBar 页面，并关闭其他所有非 tabBar 页面。
						uni.switchTab({
							url: '/pages/tabbar/mine/mine'
						});

					} else {
						uni.showToast({
							title: "密码错误，登录失败",
							icon: 'error'
						})

					}
				})



			},

			// 用户注册
			gotoRegister() {
				// uni.redirectTo :关闭当前页面，跳转到应用内的某个页面(非tabBar页面)。
				uni.redirectTo({
					url: `/pages/register/register`
				});
			},
			// 隐私协议
			handlePrivacy() {
				let site = this.globalConfig.appInfo.agreements[0]
				this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
			},
			// 用户协议
			handleUserAgrement() {
				let site = this.globalConfig.appInfo.agreements[1]
				this.$tab.navigateTo(`/pages/common/webview/index?title=${site.title}&url=${site.url}`)
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: #eeeeee;
	}

	.text-blue {
		color: #0081ff;
	}

	.normal-login-container {
		width: 100%;

		.logo-content {
			width: 100%;
			font-size: 21px;
			text-align: center;
			padding-top: 15%;

			image {
				border-radius: 4px;
			}

			.title {
				margin-left: 10px;
			}
		}

		.login-form-content {
			text-align: center;
			margin: 20px auto;
			margin-top: 15%;
			width: 80%;

			.input-item {
				margin: 20px auto;
				background-color: #f5f6f7;
				height: 45px;
				border-radius: 10px;

				.icon {
					font-size: 38rpx;
					margin-left: 10px;
					color: #999;
				}

				.input {
					width: 100%;
					height: 40px;
					font-size: 14px;
					line-height: 40px;
					text-align: left;
					padding-left: 15px;
				}

			}

			.login-btn {
				margin-top: 40px;
				height: 45px;
			}

			.reg {
				margin-top: 15px;
			}

			.xieyi {
				color: #333;
				margin-top: 20px;
			}


		}
	}
</style>